﻿using System;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace AdbTools.View
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        // 成员变量
        private Rect _normalWindowRect;
        // 添加成员变量记录原始边距
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        static extern bool SetProcessDPIAware();

        public MainWindow()
        {
            //if (Environment.OSVersion.Version.Major >= 6) SetProcessDPIAware();
            InitializeComponent();
            ApplyTheme("Themes/Dark_Black.xaml"); // 初始化主题
            //ApplyTheme("Themes/Light_white.xaml"); // 初始化主题
            //ApplyTheme("Themes/Dark_Grey.xaml"); // 初始化主题
            AdaptLayoutForMaximizedState();
        }

        public void CloseWin()
        {
            Close();
        }

        /// <summary>
        /// 鼠标左键拖动事件，通过鼠标左键可以拖动窗口
        /// </summary>
        private void WinMove_LeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Dispatcher.Invoke(delegate
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    DragMove();
                }
            });
        }

        /// <summary>
        /// 鼠标左键拖动事件，通过鼠标左键可以拖动窗口
        /// </summary>
        private void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2 && WindowState != WindowState.Maximized)
            {
                ToggleWindowState();
            }
            else
            {
                DragMove();
            }
        }

        /// <summary>
        /// 关闭主窗口事件
        /// </summary>
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            Dispatcher.Invoke(CloseWin);
        }

        /// <summary>
        /// 最大化/恢复按钮点击事件
        /// </summary>
        private void MaxRestoreButton_Click(object sender, RoutedEventArgs e)
        {
            ToggleWindowState();
        }

        /// <summary>
        /// 状态切换核心方法
        /// </summary>
        private void ToggleWindowState()
        {
            if (WindowState == WindowState.Maximized)
            {
                // 恢复窗口时保留原始尺寸
                WindowState = WindowState.Normal;
                RestoreWindowPosition();
            }
            else
            {
                // 记录恢复位置
                _normalWindowRect = new Rect(Left, Top, Width, Height);
                WindowState = WindowState.Maximized;
            }
        }

        /// <summary>
        /// 窗口位置恢复逻辑
        /// </summary>
        private void RestoreWindowPosition()
        {
            Width = _normalWindowRect.Width;
            Height = _normalWindowRect.Height;
            Left = _normalWindowRect.Left;
            Top = _normalWindowRect.Top;
        }

        /// <summary>
        /// 处理窗口状态变化时的布局适配
        /// </summary>
        protected override void OnStateChanged(EventArgs e)
        {
            base.OnStateChanged(e);
            AdaptLayoutForMaximizedState();
        }

        /// <summary>
        /// 窗口布局调整适配，可以通过鼠标调整窗体大小布局
        /// </summary>
        private void AdaptLayoutForMaximizedState()
        {
            if (WindowState == WindowState.Maximized)
            {
                // 消除系统边框间隙
                Margin = new Thickness(6);
                ResizeMode = ResizeMode.NoResize;
            }
            else
            {
                Margin = new Thickness(0);
                ResizeMode = ResizeMode.CanResizeWithGrip;
            }
        }

        /// <summary>
        /// 窗口调整平滑过渡动画
        /// </summary>
        private void AnimateWindowStateChange()
        {
            var duration = new Duration(TimeSpan.FromMilliseconds(300));
            var easing = new CubicEase { EasingMode = EasingMode.EaseOut };

            var widthAnimation = new DoubleAnimation(ActualWidth,
                WindowState == WindowState.Maximized ? SystemParameters.WorkArea.Width : _normalWindowRect.Width,
                duration)
            { EasingFunction = easing };

            var heightAnimation = new DoubleAnimation(ActualHeight,
                WindowState == WindowState.Maximized ? SystemParameters.WorkArea.Height : _normalWindowRect.Height,
                duration)
            { EasingFunction = easing };

            BeginAnimation(WidthProperty, widthAnimation);
            BeginAnimation(HeightProperty, heightAnimation);
        }

        /// <summary>
        /// 主窗口缩小到任务栏事件
        /// </summary>
        private void MinButton_Click(object sender, RoutedEventArgs e)
        {
            Dispatcher.Invoke(delegate
            {
                WindowState = WindowState.Minimized;
            });
        }
        private static readonly Brush RedBrush = Brushes.Red;
        private static readonly Brush BlueBrush = Brushes.Blue;

        /// <summary>
        /// 切换到灰色主题
        /// </summary>
        private void GreyThemeMenuItem_Click(object sender, RoutedEventArgs e)
        {
            ApplyTheme("Themes/Dark_Grey.xaml");
            string text = "GreyThemeMenuItem_Click";
            AppendColoredText(text, RedBrush);
        }

        /// <summary>
        /// 切换到白色主题
        /// </summary>
        private void LightThemeMenuItem_Click(object sender, RoutedEventArgs e)
        {
            ApplyTheme("Themes/Light_white.xaml");
            string text = "LightThemeMenuItem_Click";
            AppendColoredText(text, RedBrush);
        }

        /// <summary>
        /// 切换到深色主题
        /// </summary>
        private void DarkThemeMenuItem_Click(object sender, RoutedEventArgs e)
        {
            ApplyTheme("Themes/Dark_Black.xaml");
            string text = "DarkThemeMenuItem_Click";
            AppendColoredText(text, BlueBrush);
        }

        /// <summary>
        /// 应用主题
        /// </summary>
        private void ApplyTheme(string themeName)
        {
            // 清除当前的资源字典
            Resources.MergedDictionaries.Clear();

            // 加载新的主题资源字典
            var theme = new ResourceDictionary();
            theme.Source = new Uri(themeName, UriKind.Relative);
            Resources.MergedDictionaries.Add(theme);
        }

        
        private void AppendColoredText(string text, Brush color)
        {
            // 创建一个新的段落
            Paragraph paragraph = new Paragraph();

            // 创建一个 Run 对象，并设置文本和颜色
            Run run = new Run(text);
            run.Foreground = color;

            // 将 Run 添加到段落中
            paragraph.Inlines.Add(run);

            // 将段落添加到 RichTextBox 的文档中
            LogRichTextBox.Document.Blocks.Add(paragraph);
        }

        
    }
}